package com.dragon.stack;

/**
 * @author dragon
 * @version 1.0
 * @description
 * @date created in 2018-12-12
 */
public class LeetCodeTest {

    /**
     * 判断该字符串是否为成对的,例如:([{}]) 是成对的,为true;([(] 不是成对的,为false
     *
     * @param s
     * @return
     */
    public static boolean isValid(String s) {
        if (s == null) {
            throw new IllegalArgumentException("参数不能为null");
        }
        ArrayStack<Character> stack = new ArrayStack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if ('[' == c || '{' == c || '(' == c) {
                stack.push(c);
            } else {
                if (stack.isEmpty()) {
                    return false;
                }
                Character topChar = stack.pop();
                if (topChar == '[' && c != ']') {
                    return false;
                }
                if (topChar == '{' && c != '}') {
                    return false;
                }
                if (topChar == '(' && c != ')') {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(isValid("[](){}}"));
        System.out.println(isValid("{[({})]}"));
    }
}
